﻿IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'proc_cm_SaveBlogEntry')
BEGIN
    PRINT 'Dropping Procedure proc_cm_SaveBlogEntry'
    DROP  Procedure  proc_cm_SaveBlogEntry
END
GO

PRINT 'Creating Procedure proc_cm_SaveBlogEntry'
GO

CREATE PROCEDURE [dbo].[proc_cm_SaveBlogEntry]
	@pBlogId uniqueidentifier
	, @pHtmlPageId uniqueidentifier
	, @pTitleTxt nvarchar(512)
	, @pSubtitleTxt nvarchar(512) = null
	, @pContentTxt nvarchar(max)
	, @pUserName nvarchar(255)
AS
BEGIN
	declare @vUserId uniqueidentifier

	select @vUserId = UserID
	from aspnet_Users
	where UserName = @pUserName

	update
		[dbo].[tbl_blog_entry]
	set
		[title_txt] = @pTitleTxt
		,[subtitle_txt] = @pSubtitleTxt
		,[content_txt] = @pContentTxt
		,[update_dttm] = GETDATE()
	where
		[blog_id] = @pBlogId
		and [html_page_id] = @pHtmlPageId

	if @@ROWCOUNT = 0
	begin
		INSERT INTO [dbo].[tbl_blog_entry]
		(
			[blog_id]
			,[html_page_id]
			,[title_txt]
			,[subtitle_txt]
			,[content_txt]
			,[UserId]
			,[create_dttm]
			,[update_dttm]
		)
		SELECT
			@pBlogId
			, @pHtmlPageId
			, @pTitleTxt
			, @pSubtitleTxt
			, @pContentTxt
			, @vUserId
			, GETDATE()
			, GETDATE()
	end
END
GO

GRANT EXEC ON dbo.proc_cm_SaveBlogEntry TO PUBLIC
GO

